package zzhyc.keystore

import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.kwad.sdk.api.KsAdSDK
import com.kwad.sdk.api.KsLoadManager
import com.kwad.sdk.api.KsScene
import com.kwad.sdk.api.KsSplashScreenAd
import com.kwad.sdk.api.KsSplashScreenAd.SplashScreenAdInteractionListener
import zzhyc.keystore.databinding.ActivityShouKuaiSplashBinding

private const val TAG = "ShouKuaiSplashActivity"

class ShouKuaiSplashActivity : AppCompatActivity() {

    private val binding by lazy { ActivityShouKuaiSplashBinding.inflate(layoutInflater) }
    private var canJump = false
    private var logStr = StringBuilder()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initSplash()
    }

    private fun initSplash() {
        val ksScene = KsScene.Builder(ShouKuaiKey.KS_SPLASH_ID).build()
        KsAdSDK.getLoadManager().loadSplashScreenAd(ksScene, object :
            KsLoadManager.SplashScreenAdListener {
            override fun onError(p0: Int, p1: String?) {
                val logs = "快手开屏/onError: ${p0}, msg-- ${p1} \n"
                setLogsView(logs)
                binding.shouKuaiSplashLlTips.visibility = View.VISIBLE
                startNewActivity()
            }

            override fun onRequestResult(p0: Int) {
                val logs = "快手开屏/onRequestResult/开屏广告广告请求填充: ${p0} \n"
                setLogsView(logs)
            }

            override fun onSplashScreenAdLoad(p0: KsSplashScreenAd?) {
                setLogsView("快手开屏/onSplashScreenAdLoad: \n")
                val adView = p0?.getView(
                    this@ShouKuaiSplashActivity, object : SplashScreenAdInteractionListener {
                        override fun onAdClicked() {
                            setLogsView("快手开屏/onAdClicked: \n")
                        }

                        override fun onAdShowError(p0: Int, p1: String?) {
                            setLogsView("快手开屏/onAdShowError: ${p0}，msg-- ${p1} \n")
                            startNewActivity()
                        }

                        override fun onAdShowEnd() {
                            setLogsView("快手开屏/onAdShowEnd: \n")
                            startNewActivity()
                        }

                        override fun onAdShowStart() {
                            setLogsView("快手开屏/onAdShowStart: \n")
                            binding.shouKuaiSplashLlTips.visibility = View.GONE
                        }

                        override fun onSkippedAd() {
                            setLogsView("快手开屏/onSkippedAd: \n")
                            startNewActivity()
                        }

                        override fun onDownloadTipsDialogShow() {
                            setLogsView("快手开屏/onDownloadTipsDialogShow: \n")
                        }

                        override fun onDownloadTipsDialogDismiss() {
                            setLogsView("快手开屏/onDownloadTipsDialogDismiss: \n")
                        }

                        override fun onDownloadTipsDialogCancel() {
                            setLogsView("快手开屏/onDownloadTipsDialogCancel: \n")
                        }
                    })

                if (!isFinishing) {
                    binding.shouKuaiSplashFl.removeAllViews()
                    adView?.layoutParams = ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
                    )
                    binding.shouKuaiSplashFl.addView(adView)
                }
            }
        })
    }

    override fun onResume() {
        super.onResume()
        if (canJump) {
            startNewActivity()
        }
    }

    override fun onPause() {
        super.onPause()
        canJump = true
    }

    fun startNewActivity() {
        ShouKuaiInfoActivity.startActivity(this, logStr.toString())
        finish()
    }

    private fun setLogsView(logs: String) {
        Log.i(TAG, logs)
        logStr.append(logs)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}